/*
	@ 经销商列表
	@ 功能： 经销商查看 经销商添加
*/

var adminLoginedInfo = Cookies.getJSON('kplus_ad_logined_info');
var dictData = Cookies.getJSON('kplus_web_dict');

//dom 加载完成
$(function(){
	layui.use(['layer', 'table', 'form'], function(){
		var layer = layui.layer,  table = layui.table, form = layui.form;

		var ajaxData = { aId: adminLoginedInfo.aId, aToken: adminLoginedInfo.aToken };	

		function mergeTableData(resData){
			/*
				@ 构建table数据
				@ resData 原始ajax数据
			*/
			var table_data = new Array();
			try{
				resData.map(function(item, index){
					table_data.push({
						agentId: item.w_agt,
						agentCode: item.agt_code,
						agentName: item.agt_name['cn'],
						agentAddr: item.agt_addr['cn'],
						agentPhone: item.agt_phone,
						agentOpenTime: item.agt_open_daily,
						createTime: formatTime(item.format_time),
						admin: item.create_admin,
						status: item.agt_status
					});
				})
			}catch(err){
				console.log(err + 'resData should be array');
				return [];
			};
			return table_data; 	
		}
		function createAgentTable(tableData){
			/*
				@ 创建表格dom结构
				@ tableData 表格格式化数据
			*/

			table.render({
				elem: '#tableContent',
				limit: 30, //分页
				height: 680,
				page: true,
				cols:[[ //表头
					{field: 'agentId', title: '经销商ID', sort: true, fixed: 'left', width:80},
					{field: 'agentCode', title: '经销商编码', sort: true , width:80},
					{field: 'agentName', title: '经销商', sort: true} ,
					{field: 'agentAddr', title: '所在地址'},
					{field: 'agentPhone', title: '联系方式'},
					{field: 'agentOpenTime', title: '营业时间', sort: true},
					{field: 'createTime', title: '加入时间',sort: true},
					{field: 'admin', title: '创建ADMIN', width:80,sort: true},
					{field: 'status', title: '状态', width:80,sort: true},
					{field: 'option', title: '操作', toolbar:'#toolbar'}
				]],
				data:tableData
			});
		}  

		function getAgentsData(filter){
			/*
				@ 获取全部经销商信息
				@ filter 筛选信息
			*/
			var loadIndex = layer.load(1, { shade: [0.5, '#000'] });
			var _filter = filter || {}; 
			var _ajaxData = $.extend({}, ajaxData, _filter); 
			$.ajax({
				url: BASE_SERVICEURL + URlS.agent,
				type: 'GET',
				data:_ajaxData,
				success: function(responseData){
					layer.close(loadIndex);
					if(responseData.msg.code == 200){
						createAgentTable(mergeTableData(responseData.data.data));
						//console.log(responseData)
					}else{
						layer.msg(responseData.msg.msg, {time: 2000});
					}
				},
				error: function(responseData){
					layer.close(loadIndex);
					layer.msg(MESSAGES.service_error, {time: 2000});
				}
			})
		};
		table.on('tool(agent-list-table)', function(obj){
			/*
				@ 监听 table 操作栏点击
				@ 查看详情
				@ agent-list-table 对应 lay-filter 
			*/
			if(obj.event == 'seeDetail'){
				x_admin_show('查看详情', 'agent-detail.html?wId='+ obj.data.agentId, 1200, 800);
			};
			return false;
		});
		form.on('submit(search)', function(data){
			/*
				@ 以filter筛选信息
				@ startT endT w_agt
			*/
			getAgentsData(data.field);
			return false;
		});

		getAgentsData();
	});
})





